package com.dlna.client.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.dlna.client.R;
import com.dlna.client.enity.FileInfo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class ImagePagerAdapter extends PagerAdapter {
	private DisplayImageOptions options;
	private ArrayList<FileInfo> fileInfos;

	private LayoutInflater inflater;
	private int type;

	public ImagePagerAdapter(Context context, ArrayList<FileInfo> images,
			int type) {
		this.fileInfos = images;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.type = type;
		if (type == 0) {
			options = new DisplayImageOptions.Builder()
					.resetViewBeforeLoading(true)
					.showImageForEmptyUri(R.drawable.ic_empty)
					.showImageOnFail(R.drawable.ic_error)
					.cacheInMemory(true)
					.bitmapConfig(Bitmap.Config.RGB_565).build();
		} else {
			options = new DisplayImageOptions.Builder()
					.resetViewBeforeLoading(true)
					.showImageForEmptyUri(R.drawable.ic_empty)
					.showImageOnFail(R.drawable.ic_error)
					.cacheInMemory(true)
					.cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565)
					.build();
		}
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public void finishUpdate(View container) {
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	@Override
	public int getCount() {
		return fileInfos.size();
	}

	@Override
	public Object instantiateItem(ViewGroup view, int position) {
		View imageLayout = inflater.inflate(R.layout.item_pager_image, view,
				false);
		ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
		final ProgressBar spinner = (ProgressBar) imageLayout
				.findViewById(R.id.loading);

		FileInfo fileInfo = fileInfos.get(position);
		String url = "";

		if (type == 0) { // local
			url = "file://" + fileInfo.filePath;
		} else {
			if (fileInfo.httpUrl != null) // remote
				url = fileInfo.httpUrl;
			else if (fileInfo.filePath != null) // local
				url = "file://" + fileInfo.filePath;
		}

		ImageLoader.getInstance().displayImage(url, imageView, options,
				new SimpleImageLoadingListener() {

					@Override
					public void onLoadingStarted(String imageUri, View view) {
						spinner.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						spinner.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
//						if (loadedImage.getHeight() > loadedImage.getWidth()) {
//							ImageView imgview = (ImageView) view;
//							Matrix matrix = new Matrix();
//							matrix.postScale(1f, 1f);
//							matrix.postRotate(-90);
//							imgview.setImageBitmap(Bitmap.createBitmap(
//									loadedImage, 0, 0, loadedImage.getWidth(),
//									loadedImage.getHeight(), matrix, true));
//						} else {
//
//						}
						spinner.setVisibility(View.GONE);
					}
				});
		((ViewPager) view).addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View container) {
	}
}